package com.jsnkj.basic.dialog

import android.app.Activity
import android.app.Dialog
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.jsnkj.basic.R
import com.jsnkj.basic.util.QRCodeUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition

//import com.tb.mob.bean.Position


/**
 * 分享 邀请弹窗
 */
class JoinPlayerDialog(
    var dialogContext: Activity,
    var content: String,
    var click: onItemClickListener
) : Dialog(dialogContext), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_join_play)
        setCanceledOnTouchOutside(true)
        setCancelable(true)
        window?.setBackgroundDrawableResource(android.R.color.transparent)
        initView()
    }

    var bitmapQR: Bitmap? = null
    fun initView() {
        Glide.with(dialogContext).asBitmap().load(R.mipmap.logo)
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?
                ) {

                    bitmapQR = QRCodeUtil.createQRCodeWithLogo(content, resource)

                    val imgQr: ImageView = findViewById(R.id.img_qr)
                    Glide.with(dialogContext).load(bitmapQR).into(imgQr)
                }

                override fun onLoadCleared(placeholder: Drawable?) = Unit
            })


        val imgClose: ImageView = findViewById(R.id.img_close)
        imgClose.setOnClickListener(this)

        val tvSave: TextView = findViewById(R.id.txt_save_image)
        tvSave.setOnClickListener(this)


        var frameLayoutAd: FrameLayout = findViewById(R.id.fl_ad)

        //加载信息流
//        AdvertisementManager.loadFeed(
//            dialogContext, 300, 169,
//            object : com.tb.mob.TbManager.FeedLoadListener {
//                override fun onFail(p0: String?) {
//                    BurialPointUtil.flowAdEvent("fail")
//                }
//
//                override fun onLoad(feedPosition: FeedPosition) {
//                    BurialPointUtil.flowAdEvent("show")
//                    feedPosition.showFeed(dialogContext, frameLayoutAd)
//                }
//
//                override fun onDismiss() {
//
//                }
//
//                override fun onClicked() {
//
//                }
//
//                override fun onExposure(position: Position?) {
//                    position?.let {
//                        RewardsUtil.addGoldNumber(it.ecpm, RewardsConfig.adStream)
//                    }
//                }
//
//                override fun onVideoReady() {
//
//                }
//
//                override fun onVideoComplete() {
//
//                }
//
//            })
    }


    interface onItemClickListener {
        fun saveItemClick(bitmap: Bitmap?)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.img_close -> {
                dismiss()
            }

            R.id.txt_save_image -> {
                dismiss()
                click.saveItemClick(bitmapQR)
            }
        }
    }
}